/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mb.product.ui;

import com.mb.main.MyDefaultTableCellRenderer;
import com.mb.main.MyTable;
import com.mb.product.bean.Product;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author DUYANH
 */
public class ProductTablePanel extends javax.swing.JPanel {

    private MyTable productTable;
    private ProductPanelModel panelModel;
    
    /**
     * Creates new form ProductTable
     */
    public ProductTablePanel(ProductPanelModel panelModel) {
        this.panelModel = panelModel;
        initComponents();
        renamePopupMenuItem();
        changedMenuItemState(false);
        productTable = new MyTable(this.panelModel.getTableModel());
        productTable.setDefaultRenderer(String.class, new MyDefaultTableCellRenderer());
        productTable.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3) {
                    changedMenuItemState(productTable.getSelectedRowCount() > 0);
                    popupMenu.show(productTable, e.getX(), e.getY());
                }
            }
            
        });
        adjustTableColumnWitdh();
        this.add(new JScrollPane(productTable), BorderLayout.CENTER);
    }
    
    private void adjustTableColumnWitdh() {
        if (productTable == null)
            return;
        TableColumnModel columnModel = productTable.getColumnModel();
        for (int i = 0; i < columnModel.getColumnCount(); i++) {
            TableColumn column = columnModel.getColumn(i);
            if (((ProductPanelModel)panelModel).STT.equals(String.valueOf(column.getHeaderValue()))) {
                column.setPreferredWidth(20);
            } else if (((ProductPanelModel)panelModel).SO_LUONG.equals(String.valueOf(column.getHeaderValue()))) {
                column.setPreferredWidth(150);
            }
        }
    }
    
    private void renamePopupMenuItem() {
        menuItemAdd.setText("Thêm sản phẩm mới");
        menuItemEdit.setText("Sửa thông tin");
        menuItemRefresh.setText("Làm mới");
        menuItemDelete.setText("Xóa");
    }
    
    private void changedMenuItemState(boolean isEnabled) {
        menuItemEdit.setEnabled(isEnabled);
        menuItemDelete.setEnabled(isEnabled);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        menuItemAdd = new javax.swing.JMenuItem();
        menuItemEdit = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuItemRefresh = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        menuItemDelete = new javax.swing.JMenuItem();

        menuItemAdd.setText("jMenuItem1");
        menuItemAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAddActionPerformed(evt);
            }
        });
        popupMenu.add(menuItemAdd);

        menuItemEdit.setText("jMenuItem1");
        menuItemEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemEditActionPerformed(evt);
            }
        });
        popupMenu.add(menuItemEdit);
        popupMenu.add(jSeparator1);

        menuItemRefresh.setText("jMenuItem1");
        menuItemRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemRefreshActionPerformed(evt);
            }
        });
        popupMenu.add(menuItemRefresh);
        popupMenu.add(jSeparator2);

        menuItemDelete.setText("jMenuItem1");
        popupMenu.add(menuItemDelete);

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    private void menuItemRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemRefreshActionPerformed
        // TODO add your handling code here:
        panelModel.refresh();
    }//GEN-LAST:event_menuItemRefreshActionPerformed

    private void menuItemAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAddActionPerformed
        // TODO add your handling code here:
        ProductFormDialog addDialog = new ProductFormDialog(new JFrame(), true, panelModel.getApp(), null);
        addDialog.setPreferredSize(new Dimension(390, 400));
        addDialog.setVisible(true);
    }//GEN-LAST:event_menuItemAddActionPerformed

    private void menuItemEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemEditActionPerformed
        // TODO add your handling code here:
        String productId = String.valueOf(productTable.getValueAt(productTable.getSelectedRow(), 1));
        Product selectedProduct = panelModel.getApp().getMapProduct().get(productId);
        ProductFormDialog editDialog = new ProductFormDialog(new JFrame(), true, panelModel.getApp(), selectedProduct);
        editDialog.setPreferredSize(new Dimension(390, 400));
        editDialog.setVisible(true);
    }//GEN-LAST:event_menuItemEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JMenuItem menuItemAdd;
    private javax.swing.JMenuItem menuItemDelete;
    private javax.swing.JMenuItem menuItemEdit;
    private javax.swing.JMenuItem menuItemRefresh;
    private javax.swing.JPopupMenu popupMenu;
    // End of variables declaration//GEN-END:variables
}
